import axios from "axios";
import router from "@/router";
import qs from "qs";

let instance = axios.create();

instance.interceptors.request.use((config) => {
    let loginInfoString = localStorage.getItem('loginInfo');
    if (!loginInfoString) {
        alert(loginInfoString);
        router.push('/login');
        return config;
    }
    // console.log(JSON.parse(loginInfoString));
    config.headers.Authorization = JSON.parse(loginInfoString).token;
    return config;
})

const TokenAxios={
    get(url, params) {
        return instance({
            method: 'GET',
            url: url,
            data: params
        });
    },
    post(url, params) {
        return instance({
            method: 'POST',
            url: url,
            // data: params
            data: qs.stringify(params)
        });
    }
}
export default TokenAxios